unit uERP_Browser;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, dxSkinsCore, dxSkinBlack, dxSkinMcSkin, dxSkinsdxBarPainter,
  cxGraphics, cxControls, cxLookAndFeels, cxLookAndFeelPainters, cxStyles,
  dxSkinscxPCPainter, cxCustomData, cxFilter, cxData, cxDataStorage,
  cxEdit, cxNavigator, DB, cxDBData, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxGridCustomView, cxGrid,
  dxBar, Uni, cxEditRepositoryItems, dxSkinBlue, dxSkinBlueprint,
  dxSkinCaramel, dxSkinCoffee, dxSkinDarkRoom, dxSkinDarkSide,
  dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle, dxSkinFoggy,
  dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary, dxSkinLilian,
  dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMetropolis,
  dxSkinMetropolisDark, dxSkinMoneyTwins, dxSkinOffice2007Black,
  dxSkinOffice2007Blue, dxSkinOffice2007Green, dxSkinOffice2007Pink,
  dxSkinOffice2007Silver, dxSkinOffice2010Black, dxSkinOffice2010Blue,
  dxSkinOffice2010Silver, dxSkinOffice2013DarkGray,
  dxSkinOffice2013LightGray, dxSkinOffice2013White, dxSkinPumpkin,
  dxSkinSeven, dxSkinSevenClassic, dxSkinSharp, dxSkinSharpPlus,
  dxSkinSilver, dxSkinSpringTime, dxSkinStardust, dxSkinSummer2008,
  dxSkinTheAsphaltWorld, dxSkinsDefaultPainters, dxSkinValentine,
  dxSkinVS2010, dxSkinWhiteprint, dxSkinXmas2008Blue, NasajonSkin,
  NasajonSkinDestaque;

type
  TBrowser = class(TForm)

    {$REGION '<< Components and Events >>'}

        dxBarManager: TdxBarManager;
        tbBasicas: TdxBar;
        tbMenu: TdxBar;
        dxBarSair: TdxBar;
        btCriar: TdxBarLargeButton;
        btAlterar: TdxBarLargeButton;
        btCancelar: TdxBarLargeButton;
        btExcluir: TdxBarLargeButton;
        btSair: TdxBarLargeButton;
    btSelecionar: TdxBarLargeButton;
        cxg_Grid: TcxGrid;
        cxg_GridDBTableView: TcxGridDBTableView;
        cxg_GridLevel: TcxGridLevel;
        cxEditRepository: TcxEditRepository;
        erTextEdit: TcxEditRepositoryTextItem;
        erCheckbox: TcxEditRepositoryCheckBoxItem;
        btExtra: TdxBarLargeButton;

        procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
        procedure cxg_GridDBTableViewDblClick(Sender: TObject); virtual;
        procedure FormCreate(Sender: TObject);
        procedure FormDestroy(Sender: TObject); virtual;
        procedure cxg_GridDBTableViewCustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean); virtual;
        procedure cxg_GridDBTableViewDataControllerFilterFormatFilterTextValue(Sender: TcxDBDataFilterCriteria; AItem: TcxFilterCriteriaItem; const AValue: Variant; var ADisplayValue: string);
        procedure FormClose(Sender: TObject; var Action: TCloseAction);
        procedure FormShow(Sender: TObject); virtual;

    {$ENDREGION}

    {$REGION '<< Private >>'}

      private

        {$REGION '<< Atributes >>'}



        {$ENDREGION}

        {$REGION '<< Methods >>'}

          {$REGION '<< Events >>'}

            procedure Ev_StateChange(Sender: TObject);

          {$ENDREGION}

          procedure ConfigureToolBar();

        {$ENDREGION}

    {$ENDREGION}

    {$REGION '<< Public >>'}

      public

        {$REGION '<< Atributes >>'}



        {$ENDREGION}

        {$REGION '<< Methods >>'}

          {$REGION '<< Events >>'}



          {$ENDREGION}

          function GetColumn(const a_FieldName: String): TcxGridDBColumn;
          function GetGrid(): TcxGrid;
          function GetGridView(): TcxGridDBTableView;

          procedure SetVisibleButton(const a_Button: TdxBarLargeButton; const a_Visible: Boolean);

          procedure AddColumn(
                      const a_ColumnName: String;
                      const a_Caption: String;
                      const a_FieldName: String;
                      const a_FieldType: String;
                      const a_Size: Integer = 80;
                      const a_RepositoryItem: TcxEditRepositoryItem = nil);

          procedure SetDataSourceInDataController(const a_UniDataSource: TUniDataSource);

          {$REGION '<< Abstracts >>'}

            procedure New(a_Sender: TObject); virtual;
            procedure Edit(a_Sender: TObject); virtual;
            procedure Delete(a_Sender: TObject); virtual;
            procedure Select(a_Sender: TObject);virtual;
            procedure Extra(a_Sender: TObject); virtual;

            procedure Quit(a_Sender: TObject); virtual;

          {$ENDREGION}

        {$ENDREGION}

    {$ENDREGION}

  end;

implementation

uses uMiscelania, uMensagens, uHelpers;

{$R *.dfm}

{$REGION '<< TBrowser >>'}

procedure TBrowser.AddColumn(
                      const a_ColumnName: String;
                      const a_Caption: String;
                      const a_FieldName: String;
                      const a_FieldType: String;
                      const a_Size: Integer;
                      const a_RepositoryItem: TcxEditRepositoryItem);
var
  t_Column: TcxGridDBColumn;
begin
  t_Column := Self.GetGridView().CreateColumn();
  t_Column.Name := 'Column' + a_ColumnName;
  t_Column.Caption := a_Caption;
  t_Column.Width := a_Size;

  t_Column.DataBinding.FieldName := a_FieldName;
  t_Column.DataBinding.ValueType := a_FieldType;

  t_Column.Options.Editing := False;

  if not Assigned(a_RepositoryItem)
    then t_Column.RepositoryItem := erTextEdit
    else t_Column.RepositoryItem := a_RepositoryItem;
end;

function TBrowser.GetColumn(const a_FieldName: String): TcxGridDBColumn;
begin
  Result := Self.GetGridView().GetColumnByFieldName(a_FieldName);
end;

function TBrowser.GetGrid: TcxGrid;
begin
  Result := Self.cxg_Grid;
end;

function TBrowser.GetGridView: TcxGridDBTableView;
begin
  Result := Self.cxg_GridDBTableView;
end;

procedure TBrowser.New(a_Sender: TObject);
begin
  cxg_GridDBTableView.DataController.RefreshExternalData();
end;

procedure TBrowser.Quit(a_Sender: TObject);
begin
  Self.Close();
end;

procedure TBrowser.ConfigureToolBar;
begin
  TMiscelania.GetInstance().ConfigureStateChange(Self.GetGridView(), Self.btCriar, Self.btAlterar, Self.btExcluir, Self.btSair);

  if btSelecionar.Visible = ivAlways 
    then btSelecionar.Enabled := not (Self.cxg_GridDBTableView.DataController.DataSource.State in [dsInsert, dsEdit]) and (Self.cxg_GridDBTableView.DataController.RecordCount > 0);

  if btExtra.Visible = ivAlways 
    then btExtra.Enabled := not (Self.cxg_GridDBTableView.DataController.DataSource.State in [dsInsert, dsEdit]) and (Self.cxg_GridDBTableView.DataController.RecordCount > 0);    
end;

procedure TBrowser.cxg_GridDBTableViewCustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
  if AViewInfo.GridRecord.Selected then
    begin
      ACanvas.Brush.Color := clInactiveCaption;
      ACanvas.Font.Color := clBlack;
      ACanvas.Font.Style := [fsBold];
    end
end;

procedure TBrowser.cxg_GridDBTableViewDataControllerFilterFormatFilterTextValue(Sender: TcxDBDataFilterCriteria; AItem: TcxFilterCriteriaItem; const AValue: Variant; var ADisplayValue: string);
begin
  ADisplayValue := QuotedStr('%' + THelperVariant.ToString(AValue) + '%');
end;

procedure TBrowser.cxg_GridDBTableViewDblClick(Sender: TObject);
begin
  if Self.GetGridView().DataController.RowCount = 0 then
    begin
      TDialog.Send('Nenhum registro selecionado.', MB_OK + MB_ICONWARNING);
      Abort;
    end
  else
    begin
      if btSelecionar.Visible = ivAlways
        then Self.Select(nil)
        else Self.Edit(nil);
    end;
end;

procedure TBrowser.Delete(a_Sender: TObject);
begin
  if Self.cxg_GridDBTableView.DataController.RecordCount = 0 then
    begin
      TDialog.Send('Nenhum registro selecionado.', MB_OK + MB_ICONWARNING);
      Abort;
    end
  else if TDialog.Send('Deseja excluir este registro?', MB_YESNO + MB_ICONQUESTION) = ID_YES then
    begin
      try
        Screen.Cursor := crSQLWait;
        cxg_GridDBTableView.DataController.DeleteSelection();
      finally
        cxg_GridDBTableView.DataController.RefreshExternalData();
        Screen.Cursor := crDefault;
      end;
    end;
end;

procedure TBrowser.Edit(a_Sender: TObject);
begin
  cxg_GridDBTableView.DataController.RefreshExternalData();
end;

procedure TBrowser.Ev_StateChange(Sender: TObject);
begin
  Self.ConfigureToolBar();
end;

procedure TBrowser.Extra(a_Sender: TObject);
begin
  cxg_GridDBTableView.DataController.RefreshExternalData();
end;

procedure TBrowser.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
  Self.GetGridView().DataController.DataSource.OnStateChange := nil;
end;

procedure TBrowser.FormCreate(Sender: TObject);
begin
  Self.btCriar.OnClick := Self.New;
  Self.btAlterar.OnClick := Self.Edit;
  Self.btExcluir.OnClick := Self.Delete;
  Self.btSelecionar.OnClick := Self.Select;
  Self.btExtra.OnClick := Self.Extra;
  Self.btSair.OnClick := Self.Quit;
end;

procedure TBrowser.FormDestroy(Sender: TObject);
begin
  Self.GetGridView().DataController.DataSource := nil;
end;

procedure TBrowser.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  if (Key = VK_ESCAPE)
    then Self.Quit(nil);
end;

procedure TBrowser.FormShow(Sender: TObject);
begin
  Self.ConfigureToolBar();
end;

procedure TBrowser.Select(a_Sender: TObject);
begin

end;

procedure TBrowser.SetDataSourceInDataController(const a_UniDataSource: TUniDataSource);
begin
  Self.GetGridView().DataController.DataSource := a_UniDataSource;
  if Assigned(Self.GetGridView().DataController.DataSource)
    then Self.GetGridView().DataController.DataSource.OnStateChange := Self.Ev_StateChange;
end;

procedure TBrowser.SetVisibleButton(const a_Button: TdxBarLargeButton; const a_Visible: Boolean);
begin
  if not a_Visible
    then a_Button.Visible := ivNever
    else a_Button.Visible := ivAlways;
end;

{$ENDREGION}

end.
